function open_close() {
    return new Promise(function(resolve, reject){
        setTimeout(() => {
            $('.door').off();
            $('.left_door').addClass('left_open_close');
            $('.right_door').addClass('right_open_close');
            $('.door').on( "animationend", function() {
                $('.left_door').removeClass('left_open_close');
                $('.right_door').removeClass('right_open_close');
                resolve('success');
            });
        }, 1000);
    });
}

function move(current, new_floor_dis, moveTo, moveToFloor) {
    return new Promise(function(resolve, reject){
        setTimeout(() => {
            $('.door').off();
            let move = [{
                transform: 'translateY(' + moveTo + 'px)',
            }];
            let timing = {
                duration : Math.abs(moveTo)/100*1000,
            };
            $('.left_door').get(0).animate(move,timing);
            $('.right_door').get(0).animate(move,timing);
            Promise.all(
                document.querySelector(".door").getAnimations().map(
                    function(animation) {
                        return animation.finished
                    }
                )
            ).then(
                function() {
                    $('.left_door').css('bottom', new_floor_dis + 'px');
                    $('.right_door').css('bottom', new_floor_dis + 'px');
                    current_floor = moveToFloor;
                    resolve('success');
                }
            );
        }, 1000);
    });
}

async function run(){
    let run_status = false;
    setInterval(async function(){
        if(floor_array.length !== 0 && !run_status){
            run_status = true;
            let moveToFloor = floor_array.shift();
            let current_dis = (current_floor - 1) * 100;
            let new_floor_dis = (moveToFloor - 1) * 100;
            let move_dis = -(new_floor_dis - current_dis);
            await open_close();
            await move(current_dis, new_floor_dis, move_dis, moveToFloor);
            if(floor_array.length == 0){
                await open_close();
            }
            $('#'+moveToFloor).css('background-color', 'darkgreen');
            run_status = false;
        }
    },500);
}

run();

let current_floor = 1;
let floor_array = [];
$('.keyword a').on('click',function(e){
    let moveToFloor = e.target.id;
    if(floor_array.indexOf(moveToFloor) == -1){
        floor_array.push(moveToFloor);
        $('#'+moveToFloor).css('background-color', 'gold');
    }
});